package com.winning.ds.login.filter;


import com.winning.ds.common.util.XSSUtil;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;


public class XssRequestWrappers extends HttpServletRequestWrapper{
    private String body;

    private CommonsMultipartResolver multiparResolver = new CommonsMultipartResolver();
    public XssRequestWrappers(HttpServletRequest request) {
        super(request);

        String type = request.getHeader("Content-Type");
      /*  if (!StringUtils.isEmpty(type) && type.contains("multipart/form-data")) {
       // if (!StringUtils.isEmpty(type)) {
            MultipartHttpServletRequest multipartHttpServletRequest = multiparResolver.resolveMultipart(request);
            Map<String, String[]> stringMap = multipartHttpServletRequest.getParameterMap();
            if (!stringMap.isEmpty()) {
                for (String key : stringMap.keySet()) {
                    String value = multipartHttpServletRequest.getParameter(key);
                    //工具类XSSUtils，现在的做法是替换成空字符
                    XSSUtil.striptXSS(key);
                    XSSUtil.striptXSS(value);
                }
            }
            super.setRequest(multipartHttpServletRequest);

        }else if (!StringUtils.isEmpty(type) && type.contains("application/x-www-form-urlencoded")){
            Map<String, String[]> stringMap = request.getParameterMap();
            if (!stringMap.isEmpty()) {
                for (String key : stringMap.keySet()) {
                    String[] value = stringMap.get(key);
                    //工具类XSSUtils，现在的做法是替换成空字符
                    XSSUtil.striptXSS(key);
                    XSSUtil.striptXSS(value[0]);
                }
            }
            super.setRequest(request);
         //   application/json application/xml
       // }else if(!StringUtils.isEmpty(type)){
        }else if(!StringUtils.isEmpty(type)){
            //request.getInputStream().readLine();
            BufferedReader br = null;
            StringBuilder sb = new StringBuilder();
            try {
                br = request.getReader();
                String str = null;
                while((str = br.readLine()) != null){
                    sb.append(str);
                }
                body = XSSUtil.striptXSS(sb.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/
        if (!StringUtils.isEmpty(type) && (type.contains("application/json")||type.contains("application/xml"))) {
            BufferedReader br = null;
            StringBuilder sb = new StringBuilder();
            try {
                br = request.getReader();
                String str = null;
                while((str = br.readLine()) != null){
                    sb.append(str);
                }
                body = XSSUtil.striptXSS(sb.toString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    @Override
    public String[] getParameterValues(String parameter) {
        String[] values = super.getParameterValues(parameter);
        if (values == null) {
            return null;
        }
        int count = values.length;
        String[] encodedValues = new String[count];
        for (int i = 0; i < count; i++) {
            encodedValues[i] = XSSUtil.striptXSS(values[i]);
        }
        return encodedValues;
    }

    @Override
    public String getParameter(String parameter) {
        String value = super.getParameter(parameter);
        return XSSUtil.striptXSS(value);
    }

    @Override
    public String getHeader(String name) {
        String value = super.getHeader(name);
        return XSSUtil.striptXSS(value);
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        Map<String, String[]> map1 = super.getParameterMap();
        Map<String, String[]> escapseMap = new HashMap<String, String[]>();
        Set<String> keys = map1.keySet();
        for (String key : keys) {
            String[] valArr = map1.get(key);
            if (valArr != null && valArr.length > 0) {
                String[] escapseValArr = new String[valArr.length];
                for (int i = 0; i < valArr.length; i++) {
                    String escapseVal = XSSUtil.striptXSS(valArr[i]);
                    escapseValArr[i] = escapseVal;
                }
                escapseMap.put(key, escapseValArr);
            }
        }

        return escapseMap;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        ByteArrayInputStream bais1 = new ByteArrayInputStream(body.getBytes());
        if(!StringUtils.isEmpty(body)){
             bais1 = new ByteArrayInputStream(XSSUtil.striptXSS(body).getBytes("UTF-8"));
        }
        final ByteArrayInputStream bais =bais1;

        return new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {

            }

            @Override
            public int read() throws IOException {
                return bais.read();
            }
        };
    }

    @Override
    public BufferedReader getReader() throws IOException {
        //XSSUtil.striptXSS();
        //Map<String, String[]> map1 = super.getParameterMap();

       // return new BufferedReader(new InputStreamReader(this.getInputStream()));

        //super.getReader();
        //return new BufferedReader(new InputStreamReader(super.getInputStream(),"UTF-8"));
        return new BufferedReader(new InputStreamReader(super.getInputStream()));

    }

}
